package com.neudu.glsb.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.neudu.glsb.dto.StatsDTO;
import com.neudu.glsb.pojo.Orders;
import com.neudu.glsb.service.OrdersService;
import com.neudu.glsb.mapper.OrdersMapper;
import com.neudu.glsb.util.PickupCodeUtil;
import org.apache.ibatis.annotations.Delete;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.Random;

/**
* @author Lenovo
* @description 针对表【orders(订单信息)】的数据库操作Service实现
* @createDate 2025-04-16 22:28:32
*/
@Service
public class OrdersServiceImpl extends ServiceImpl<OrdersMapper, Orders>
    implements OrdersService{

@Override
public List<Orders> listCompletedOrders() {
    // 修复：SQL中状态是"已完成"，与前端保持一致
    return baseMapper.selectCompletedOrders();
}

    // 修复：调用工具类生成数字字母混合取餐码
    @Override
    public String generatePickupCode() {
        return PickupCodeUtil.generateMixedCode();
    }

    @Override
    public List<Orders> getPendingPickupOrders() {
        // 调用Mapper查询待取餐订单（按取餐码升序）
        return baseMapper.selectPendingPickupOrders();
    }

    @Override
    public boolean updateToPendingPickup(Integer orderId) {
        if (orderId == null) {
            return false;
        }
        LambdaUpdateWrapper<Orders> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(Orders::getId, orderId)
                .set(Orders::getOrderStatus, "待取餐")
                .set(Orders::getPickupCode, generatePickupCode()); // 关联新生成逻辑
        return this.update(updateWrapper);
    }

    @Override
    public boolean updateToCompleted(Integer orderId) {
        if (orderId == null) {
            return false;
        }
        LambdaUpdateWrapper<Orders> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(Orders::getId, orderId)
                .set(Orders::getOrderStatus, "已完成"); // 修复：与前端状态"完成"统一为"已完成"
        return this.update(updateWrapper);
    }

    //新增加
    @Override
    public StatsDTO getSalesStatsByDateRange(String startTime, String endTime) {
        StatsDTO statsDTO = new StatsDTO();

        // 1. 获取总数据（销售额、订单数）
        Map<String, Object> totalMap = baseMapper.getTotalStatsByDateRange(startTime, endTime);
        BigDecimal totalSales = totalMap.get("total_sales") == null ? BigDecimal.ZERO : (BigDecimal) totalMap.get("total_sales");
        Integer orderCount = totalMap.get("order_count") == null ? 0 : ((Long) totalMap.get("order_count")).intValue();

        // 2. 计算平均客单价
        BigDecimal avgOrderAmount = orderCount > 0 ? totalSales.divide(BigDecimal.valueOf(orderCount), 2, BigDecimal.ROUND_HALF_UP) : BigDecimal.ZERO;

        // 3. 获取销售额趋势
        List<Map<String, Object>> salesTrend = baseMapper.getSalesTrendByDateRange(startTime, endTime);

        // 4. 获取支付方式占比
        List<Map<String, Object>> paymentRatio = baseMapper.getPaymentRatioByDateRange(startTime, endTime);

        // 5. 封装返回结果
        statsDTO.setTotalSales(totalSales);
        statsDTO.setOrderCount(orderCount);
        statsDTO.setAvgOrderAmount(avgOrderAmount);
        statsDTO.setSalesTrend(salesTrend);
        statsDTO.setPaymentRatio(paymentRatio);

        return statsDTO;
    }
}




